/**
 *Project: jh-framework
 *File: cn.xkshow.util.DateHelper.java 
 *Author: admin@xkshow.cn with the copyrite
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-1-27        xkshow.cn         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.apache.commons.logging.Log;
import org.springframework.context.i18n.LocaleContextHolder;

public class DateHelper
{
  private static Log log;
  private static String defaultDatePattern;
  private static String timePattern;

  public static String getDatePattern()
  {
    Locale locale = LocaleContextHolder.getLocale();
    try
    {
      defaultDatePattern = ResourceBundle.getBundle("applicationResources", locale).getString("date.format");
    }
    catch (MissingResourceException mse)
    {
      defaultDatePattern = "yyyy-MM-dd";
    }

    return defaultDatePattern;
  }

  public static String getDateTimePattern()
  {
    return getDatePattern() + " HH:mm:ss";
  }

  public static final String getDate(Date aDate)
  {
    SimpleDateFormat df = null;
    String returnValue = "";

    if (aDate != null)
    {
      df = new SimpleDateFormat(getDatePattern());
      returnValue = df.format(aDate);
    }

    return returnValue;
  }

  public static final Date convertStringToDate(String aMask, String strDate)
  {
    if (strDate == null)
    {
      return null;
    }

    Date date = null;
    SimpleDateFormat df = new SimpleDateFormat(aMask);
    try
    {
      date = df.parse(strDate);
    }
    catch (ParseException pe)
    {
      log.error("ParseException: " + pe);
    }

    return date;
  }

  public static String getTimeNow(Date theTime)
  {
    return getDateTime(timePattern, theTime);
  }

  public static Calendar getToday()
  {
    Date today = new Date();
    SimpleDateFormat df = new SimpleDateFormat(getDatePattern());

    String todayAsString = df.format(today);
    Calendar cal = new GregorianCalendar();
    cal.setTime(convertStringToDate(todayAsString));

    return cal;
  }

  public static final String getDateTime(String aMask, Date aDate)
  {
    SimpleDateFormat df = null;
    String returnValue = "";

    if (aDate == null)
    {
      log.error("aDate is null!");
    }
    else
    {
      df = new SimpleDateFormat(aMask);
      returnValue = df.format(aDate);
    }

    return returnValue;
  }

  public static final String convertDateToString(Date aDate)
  {
    return getDateTime(getDatePattern(), aDate);
  }

  public static final String convertDateToString(String pattern, Date aDate)
  {
    return getDateTime(pattern, aDate);
  }

  public static Date convertStringToDate(String strDate)
  {
    Date aDate = null;

    if (log.isDebugEnabled())
    {
      log.debug("converting date with pattern: " + getDatePattern());
    }

    aDate = convertStringToDate(getDatePattern(), strDate);

    return aDate;
  }
}
